package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDto;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper,CheckGroup> implements CheckGroupService {
    @Autowired
    private CheckItemService checkItemService;

    @Override
    public void saveOrUpdateCheckGroup(CheckGroupDto checkGroupDto) {
        Integer id = checkGroupDto.getId();
        saveOrUpdate(checkGroupDto);
        if(StringUtils.isEmpty(id)) {
            id = checkGroupDto.getId();
        }else {
            baseMapper.deleteCheckGroupAndCheckItemById(id);
        }
        Integer[] itemIds = checkGroupDto.getCheckitemIds();
        for (Integer itemId : itemIds) {
            baseMapper.addCheckGroupAndCheckItem(id,itemId);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        queryWrapper.eq("is_delete",0);
        queryWrapper.and(i->i.like("name",queryString==null?"":queryString).or()
                .like("code",queryString==null?"":queryString));
        Page<CheckGroup> page = page(new Page<CheckGroup>
                (queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        List<CheckGroup> checkGroupList = page.getRecords();
        long total = page.getTotal();
        PageResult pageResult = new PageResult(total,checkGroupList);
//        Map map = new HashMap();
//        map.put("list",checkGroupList);
//        map.put("total",total);
        return pageResult;
    }

    @Override
    public Map findCheckItemIdsByGroupId(int id) {
        int[] ids = baseMapper.findCheckItemIdsByGroupId(id);
        List<CheckItem> list = checkItemService.list();
        Map map = new HashMap<>();
        map.put("list",list);
        map.put("ids",ids);
        return map;
    }

    @Override
    public void deleteChenckGroupAndCheckItemById(Integer id) {
        baseMapper.deleteCheckGroupAndCheckItemById(id);
        baseMapper.deleteById(id);
    }

    @Override
    public List<CheckItem> findCheckItemsById(Integer id) {
        List<CheckItem> checkItems = baseMapper.findCheckItemsById(id);
        return checkItems;
    }
}
